import tool from '../tool'
import util from '_util/index'
import _ from 'lodash'
import in_array from 'in_array'

export default {
    refreshListData(_this) {
        let eventVue = util.store.get(_this,'eventVue');
        eventVue.$emit('refreshData');

    },
    getListOpBtns(_this, obj) {
        let arr = Object.keys(obj);
        let vals = Object.values(obj);
        util.getHardCode(_this, arr, (rs) => {
            if (rs) {
                rs.forEach((item, ind) => {
                    let attr = vals[ind];
                    // tool.cl('attr:', attr);
                    _this[attr] = item;
                });
            }

        })
    },
    get_not_use_old_search_param() {
        // return ['Trodetail', 'Trosp', 'Refundorders'];
        return [];
    },
    saveParam(_this) {
        //将搜索条件存储到vuex
        let not_use_old = this.get_not_use_old_search_param();
        let ctl = _this.ctl;
        if (!not_use_old.contains(ctl)) {
            util.store.push(_this, 'setFilter', 'filterData', ctl, _this.searchAll, 'list');

        }
    },
    save_list_data(_this) {
        //将列表数据存储到vuex
        let {ctl,items}=_this;
        util.store.push(_this, 'set_list_data', 'list_data', false, items, 'list');
    },
    rememberParam(_this, param) {

        let ctl = _this.ctl;
        let old_param = _this.$store.state.list.filterData[ctl];
        let not_use_old = this.get_not_use_old_search_param();
        if (old_param && !not_use_old.contains(ctl)) {
            Object.assign(param, old_param);

        }
    },
    addParam(_this, param) {
        let arr = [];
        switch (_this.ctl) {
            case 'Attr':
                arr = ['bind_mid'];
                break;
            case 'Mbind':
            case 'Export':
                arr = ['mid'];
                break;
            case 'Auth':
                arr = ['model_id'];
                break;
            case 'Menu':
                arr = ['is_topMenu'];
                break;
            case 'Product':
                arr = ['top_product_id'];
                break;
            case 'WarehouseTopProduct':
                arr = ['warehouse_id'];
                break;
            case 'TopProduct':
                arr = ['navigation_id'];
                break;
            case 'Whprod':
                arr = ['warehouse_top_product_id'];
                break;
            case 'Tr':
                arr = ['warehouse_id'];
                break;
            case 'Sceniclist':
                arr = ['city_code'];
                break;
            case 'Refundorders':
                arr = ['order_num', 'isSearch'];
                break;
			case 'TransferDetail':
				arr = ['withdraw_id'];
				break;
            case 'TrTopProduct':
                arr = ['hotel_id'];
                break;
            case 'Taskcom':
                arr = ['task_block'];
                break;
        }
        let query = _this.$route.query;
        arr.forEach(item => {
            if (_.has(query, item) && !_.has(param, item)) {
                param[item] = query[item];
            }
        });


    },
    get_onOff_field(_this) {
        let rs = '';
        switch (_this.ctl) {
            case 'Pattr':
            case 'Navigation':
            case 'ProductType':
            case 'Product':
            case 'TopProduct':
            case 'Member':
                rs = 'status';
                break;
            case 'WithdrawWhite':
                rs = 'is_valid';
                break;
            default:
                rs = 'ol';
                break;
        }
        return rs;
    },
    get_onOff_val(_this, flag) {
        let rs = '';
        let need_del = this.get_need_del(_this);
        switch (_this.ctl) {
            case 'Pattr':
            case 'Navigation':
            case 'TopProduct':
            case 'Product':
                rs = flag ? 1 : 5;
                rs=need_del&&flag==-1?10:rs;
                break;
            default:
                rs = flag;
                break;
        }
        return rs;
    },
    checkIds(_this,clb,msg,canEmpty=false){
        let selected_ids = util.store.get(_this, `selected_ids.${_this.ctl}`, 'list');
        let b = selected_ids && selected_ids.length > 0;
        if (b||canEmpty) {
            clb(selected_ids)
        }else{
            util.showMsg(_this, 'warning', msg||'请选择要操作的数据');
        }
    },
    need_rebuild(_this) {
        let ctl = _this.ctl;
        return ['Taskcom'].contains(ctl);
    },
    get_del_msg(_this) {
        let rs = '';
        switch (_this.ctl) {
            case 'Product':
                rs = '确定删除选中商品规格？选中商品规格将在所有已绑定的仓库和场景中下架';
                break;
            case 'TopProduct':
                rs = '确定删除选中商品及所属商品规格？选中商品及所属商品规格将在所有已绑定的仓库和场景中下架';
                break;
            default:
                rs = '确定删除选中记录？';
                break;
        }
        return rs;
    },
    save_colorConf(_this,val) {
        if(!val)
            return null;
        let arr = {};
        val.forEach(item => {
            let rule = item.rule;
            let field = item.field;
            arr[field] = {};
            let val_arr = rule.split('#');
            val_arr.forEach(v=> {
                let [value, color]=v.split('|');

                arr[field][value]=color;
            });

        });
        return arr;
        // util.store.push(_this, 'set_colorConf', 'colorConf', _this.ctl, arr, 'list');
    },
    get_need_del(_this) {
        let ctl = _this.ctl;
        // return ['Product','TopProduct'].contains(ctl);
        return [].contains(ctl);
    },
    hl_list_id(_this) {
        let rs = '';
        switch (_this.ctl) {
            case 'Withdraw':
                rs = 'withdraw_id';
                break;
            default:
                rs = 'id';
                break;
        }
        return rs;
    },
    hlCellBtnAuth(_this) {
        let auths =_this.cell_btn_auths = util.modelFn('Auth', 'get_type_auth', _this, 'cell_op');
        _this.items=_this.items.map(item=> {
            return item.map(col => {
                if(col.btns) {
                    Object.keys(col.btns).forEach(flag=> {
                        if(!auths.contains(flag)) {
                            delete col.btns[flag];
                        }
                    });
                }
                return col;
            });
        })

    },
}
